package com.mfhcd.facepay.model;

import androidx.databinding.BaseObservable;


import com.mfhcd.facepay.global.ServerUrl;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

/**
 * 服务器请求对象实体
 */
public abstract class BaseRequestModel extends BaseObservable implements Serializable {
    /**
     * SaaS公共请求参数
     */
    protected String host;
    protected String service;
    protected String api;
    protected String trace;
    //param：各module的req继承base，实现自己的param
    protected Param param;
    /**
     * PaaS公共请求参数
     */
    protected String hostName;
    protected String sysCode;
    protected String requestCode;
    protected String sysTraceNo;
    protected Param params;

    public BaseRequestModel() {
        host = ServerUrl.HOST;
        service = getService();
        api = getApi();
        trace = String.valueOf(System.currentTimeMillis());
    }

    public abstract String getBaseUrl();

    public abstract String getService();

    public abstract String getApi();

    public void setParam(Param param) {
        this.param = param;
    }

    public Param getParam() {
        return param;
    }

    public Param getParams() {
        return params;
    }

    public void setParams(Param params) {
        this.params = params;
    }

    public String constructUrl() {
        return getBaseUrl() + getService() + "/" + ServerUrl.API + getApi();
    }

    public String getHost() {
        return this.host;
    }

    public static abstract class Param extends BaseObservable implements Serializable {
    }

    /**
     * 公共签名使用
     */
    public static final class CommonReq extends BaseRequestModel {
        String host;
        String service;
        String api;

        public CommonReq(JSONObject jsonObject) throws JSONException {
            this.host = jsonObject.getString("host");
            this.service = jsonObject.getString("service");
            this.api = jsonObject.getString("api");
        }

        @Override
        public String getBaseUrl() {
            return null;
        }

        @Override
        public String getService() {
            return this.service;
        }

        @Override
        public String getApi() {
            return this.api;
        }
    }

    /**
     * 公共签名使用
     */
    public static final class PassCommonReq extends BaseRequestModel {
        String hostName;
        String sysCode;
        String requestCode;
        String sysTraceNo;

        public PassCommonReq(JSONObject jsonObject) throws JSONException {
            this.hostName = jsonObject.getString("hostName");
            this.sysCode = jsonObject.getString("sysCode");
            this.requestCode = jsonObject.getString("requestCode");
            this.sysTraceNo = String.valueOf(System.currentTimeMillis());
        }

        @Override
        public String getBaseUrl() {
            return this.constructUrl();
        }

        @Override
        public String getService() {
            return this.service;
        }

        @Override
        public String getApi() {
            return this.api;
        }
    }
}
